package com.mxpio.webapp.service.impl;

import com.mxpio.webapp.entity.MonitorNet;
import com.mxpio.webapp.service.IMonitorNetService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.initiator.JpaUtilAble;
import com.mxpioframework.jpa.query.Criteria;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 网络信息服务实现类
 *
 * @author: Cheng
 * @date: 2022/11/06
 */
@Service
public class MonitorNetServiceImpl implements IMonitorNetService, JpaUtilAble {
    @Override
    public void afterPropertiesSet(ApplicationContext applicationContext) {

    }

    @Override
    @Transactional(readOnly = false)
    public MonitorNet create(MonitorNet monitorNet) {
        JpaUtil.save(monitorNet);
        return monitorNet;
    }

    @Override
    @Transactional(readOnly = true)
    public Page<MonitorNet> queryAll(Criteria criteria, Pageable pageable) {
        return JpaUtil.linq(MonitorNet.class).where(criteria).paging(pageable);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(List<String> ids) {
        JpaUtil.lind(MonitorNet.class).in("id", ids).delete();
    }

    @Override
    @Transactional(readOnly = false)
    public void editMonitorNet(MonitorNet monitorNet) {
        //查询数据库中是否已经存在此目标主机和目标端口号的记录
        boolean exists = JpaUtil.linq(MonitorNet.class)
                .equal("ipTarget", monitorNet.getIpTarget())
                .notEqual("id", monitorNet.getId())
                .exists();
        if (exists) {
            throw new RuntimeException("重复的目标主机地址和目标端口号");
        }

        JpaUtil.update(monitorNet);
    }
}
